package com.hrpmar.service.impl;

import com.hrpmar.controller.dto.PurplanDto;
import com.hrpmar.mapper.*;
import com.hrpmar.pojo.Druginfo;
import com.hrpmar.pojo.Purdetails;
import com.hrpmar.pojo.Purplan;
import com.hrpmar.service.PurplanService;
import com.hrpmar.utils.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by c on 2017-11-17.
 */
@Service
@Transactional
public class PurplanServiceImpl implements PurplanService {
    @Resource
    private PurplanMapper purplanMapper;

    @Resource
    private PurdetailsMapper purdetailsMapper;

    @Resource
    private DruginfoMapper druginfoMapper;

    @Resource
    private ProviderMapper providerMapper;

    @Resource
    private WorkerMapper workerMapper;

    @Resource
    private DrugstockMapper drugstockMapper;

    @Override
    public Pager search(Integer offset, Integer limit, PurplanDto condition) {
        Pager pager = new Pager();
        pager.setRows(purplanMapper.searchPurPlanByCondition(offset,limit,condition));
        pager.setTotal(purplanMapper.getTotal(condition));
        return pager;
    }

    @Override
    public Purplan searchPurplanByNum(String planNum) {
        return purplanMapper.searchPurplanByNum(planNum);
    }

    @Override
    public List<Purdetails> searchDetails(String planNum) {
        return purdetailsMapper.searchDetails(planNum);
    }

    @Override
    public List<Druginfo> searchAllDrug() {
        return druginfoMapper.searchAll();
    }

    @Override
    public Map<String, List> searchProAndWorker() {
        Map<String,List> map = new HashMap<String,List>();
        map.put("pro",providerMapper.searchAll());
        map.put("worker",workerMapper.searchAllBuyer());
        return map;
    }

    @Override
    public int searchOneDrugStock(String drugNum) {
        return drugstockMapper.getStockByDrugNum(drugNum);
    }

    @Override
    public String searchNewPurNum() {
        return purplanMapper.searchLastPurNum();
    }

    @Override
    public String addDels2Map(Purdetails purdetails, HttpServletRequest req) {
        HttpSession sessions = req.getSession();
        Map<Integer,Purdetails> map=null;
        if(sessions.getAttribute("purDelMap")==null){
            map = new HashMap<Integer,Purdetails>();
            sessions.setAttribute("purDelMap",map);
        }
        map = (Map<Integer,Purdetails>) sessions.getAttribute("purDelMap");

            Integer newId = map.size()+1;
            map.put(newId,purdetails);

        //返回最新的编号
        return newId.toString();
    }

    @Override
    public void addPurplan(HttpServletRequest req, Purplan purplan) {
        HttpSession session = req.getSession();
        //获取session里的采购明细信息
        Map<Integer,Purdetails> map = (Map<Integer,Purdetails>)session.getAttribute("purDelMap");
        //添加采购主表信息
        purplan.setPlanSubdate(new Date());
        purplan.setPlanChstate(0);
        purplan.setPlanRustate(0);
        if(purplanMapper.addPurplan(purplan)>0){
            Set<Integer> keySet = map.keySet();
            //循环添加采购明细信息
            for(Integer key:keySet ){
                Purdetails purdetails = map.get(key);
                purdetailsMapper.addDetails(purdetails);
            }
            //清空map
            map.clear();
        }
    }

    @Override
    public Purdetails getPurdetails(Integer delId) {
        return purdetailsMapper.getPurdetails(delId);
    }

    @Override
    public void updateDels(Purdetails dels) {
        purdetailsMapper.updateDels(dels);
    }

    @Override
    public void delDels(Integer delId) {
        purdetailsMapper.delDels(delId);
    }
}
